package com.shxc.smarttrade.old.modal;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.old.bean.AccountInfo;
import com.shxc.smarttrade.old.bean.ForeignOrder;
import com.shxc.smarttrade.old.bean.PendOrder;
import com.shxc.smarttrade.old.bean.PositionOrderList;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;

import org.xchris.x;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/5/31.
 */

public class PositionModal implements IPositionFragment {
    /**
     * 持仓列表
     *
     * @param userId
     * @param listener
     */
    @Override
    public void getForeignOrder(String accessToken, final int userId, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.POSITION_FRAGMENT_FOREIGNORDER, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        if (!jsonObject.get("data").isJsonNull()) {
                            JsonObject data = jsonObject.get("data").getAsJsonObject();
                            Gson gson = new Gson();
                            ForeignOrder foreignOrder = gson.fromJson(data, ForeignOrder.class);
                            listener.success(foreignOrder);
                        } else {
                            listener.faild("数据异常");
                        }
                    } else {
                        listener.faild(errMsg);
                    }
                }
            }
        });
    }

    /**
     * 挂单列表
     *
     * @param userId
     * @param listener
     */
    @Override
    public void getPendingList(String accessToken, int userId, final IRequestServiceListener listener) {

        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.POSITION_FRAGMENT_PENDING_LIST, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    Gson gson = new Gson();
                    PendOrder pendOrder = gson.fromJson(response, PendOrder.class);
                    if (pendOrder.err_code.equals("0"))
                        listener.success(pendOrder);
                    else
                        listener.faild(pendOrder.err_msg);
                }
            }
        });

    }

    @Override
    public void getAccountInfo(String accessToken, int userId, final IRequestServiceListener listener) {

        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.POSITION_FRAGMENT_ACCINFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
//                Logger.e("AccountInfo::::", response);
                if (response != null) {
                    Gson gson = new Gson();
                    AccountInfo pendOrder = gson.fromJson(response, AccountInfo.class);
                    if (pendOrder.err_code.equals("0"))
                        listener.success(pendOrder);
                    else
                        listener.faild(pendOrder.err_msg);
                }
            }
        });
    }

    @Override
    public void getPositionOrderList(String accessToken, int userId, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.POSITION_FRAGMENT_POSITIONORDERLIST, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
//                Logger.e("PositionOrderList::::::", response);

                if (response != null) {
                    Gson gson = new Gson();
                    PositionOrderList pendOrder = gson.fromJson(response, PositionOrderList.class);
                    if (pendOrder.err_code.equals("0"))
                        listener.success(pendOrder);
                    else
                        listener.faild(pendOrder.err_msg);
                }
            }
        });
    }


}
